/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fxpcland;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Button;

import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Priority;
import javax.swing.JOptionPane;



import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;


import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.Event;
import javafx.scene.Node;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import lib.tools.DBClass;
import myInterface.myIndex;
import options.DataModel;

/**
 *
 * @author ismail
 */
public class SampleController implements Initializable {

    @FXML
    private TabPane tabpane;
    @FXML
    private Tab mytabAdd;
    @FXML
    private AnchorPane menuBody;
    @FXML
    private VBox vCat;
    @FXML
    private AnchorPane vItem;

    @FXML
    private void ShowHideMenu(ActionEvent event) throws IOException {



        if (menuBody.isVisible()) {
            menuBody.setVisible(false);
        } else {
            menuBody.setVisible(true);

        }

    }

    @FXML
    private void addTab(ActionEvent event) throws IOException {
        createTab("new", "/test/newtest.fxml");
        tabpane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);

        String myId = tabpane.getTabs().get(tabpane.getTabs().size() - 1).getId();

        navigateTo(myId);

    }
    int actTab = 0;

    public void navigateTo(String tab) {
        for (Tab t : tabpane.getTabs()) {
            if (tab.equals(t.getId())) {
                tabpane.getSelectionModel().select(t);

                return;
            }
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        try {
            addToCates("test");
        } catch (SQLException ex) {
            Logger.getLogger(SampleController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    myIndex index = new myIndex();

    private void addToCates(String name) throws SQLException {

        ArrayList<String> importData = index.getMenuCategories();


        for (String mcatItems : importData) {
            String[] mcatItem = mcatItems.split(";");


            final VBox ItmesPanel = new VBox();

            for (Node child : vItem.getChildren()) {
                child.setVisible(false);

            }





            ArrayList<String> importitems = index.getMenuIemes(mcatItem[0]);
            for (String importitemses : importitems) {
                final String[] importitem = importitemses.split(";");


                ImageView imageView = new ImageView("/images/a1.png" );
                imageView.setFitHeight(20);
                imageView.setFitWidth(20);
                Label label = new Label(importitem[4], imageView);
                //label.setTooltip(  importitem[5]);
                
                label.setContentDisplay(ContentDisplay.LEFT);
                ItmesPanel.getChildren().add(label);

                label.onMouseClickedProperty().set(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent t) {
                        try {
                            createTab("new", importitem[4] + ".fxml");
                        } catch (IOException ex) {
                            Logger.getLogger(SampleController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                });



            }

            ItmesPanel.setPrefWidth(vItem.getPrefWidth());
            ItmesPanel.setPrefHeight(vItem.getPrefHeight());
            vItem.getChildren().add(ItmesPanel);




            Button bt = new Button(mcatItem[1]);
            bt.getStyleClass().add("menuActBT");
            bt.setPrefWidth(vCat.getPrefWidth());
            bt.setPrefHeight(50);
            vCat.getChildren().add(bt);


            bt.onMouseClickedProperty().set(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent t) {
                    for (Node child : vItem.getChildren()) {
                        child.setVisible(false);
                    }
                    ItmesPanel.setVisible(true);


                }
            });


        }
    }

    private void createTab(String title, String url) throws IOException {

        actTab++;
        Pane c = new Pane();
        c.getChildren().add((Node) FXMLLoader.load(getClass().getResource(url)));
        Tab t = new Tab(title);
        t.setId("x" + actTab);

        t.setClosable(true);
        t.setContent(c);
        tabpane.getTabs().add(t);
    }
}
